#include "viewprocess.h"

#include <QSqlDatabase>
#include <iostream>
#include <string>
#include <QSqlQuery>
#include <QVariant>
#include <limits>

#include "datauser.h"
#include "sql.h"
#include "controlleruserconfig.h"
#include "controllerreciteword.h"

Process::Process(int lines, int cols)
{
    m_lines = lines;
    m_cols = cols;
    m_currentLine = 1;
    createWindow();
}


Process::Process()
{
    m_lines = 0;
    m_cols = 0;
    m_currentLine = 1;
    createWindow();
}
Process::~Process()
{
    delwin(m_window);
}

void Process::createWindow()
{
    m_window = newwin(m_lines, m_cols, 0, 0);
    box(m_window, '+' ,'*');
    noecho();
}
void Process::load()
{

}

void Process::rush()
{

    UserConfig& userConfig = UserConfig::getInstance();
    if(!userConfig.isUserConfiged()) {
        mvwprintw(m_window,m_currentLine++,1,"检测到用户没有设置计划，进入设置……");
        config();
    }

    if(!userConfig.isUserConfiged()) {
        mvwprintw(m_window,m_currentLine++,1,"程序异常，按任意键返回……");
        wgetch(m_window);
        return ;
    }
    User user = userConfig.getUser();
    std::string in;
    in +=  "您当前选择的词汇表为 " + user.currentVocabularyName();
    in += " ，一组 " + std::to_string(user.groupSize()) + " 个。" ;
    mvwprintw(m_window,m_currentLine++,1,in.c_str());
    int select;
    echo();
    curs_set(1);
    do {
        mvwprintw(m_window, m_currentLine++, 1 , "模式：1.跟写模式 2.拼写模式");
        mvwprintw(m_window, m_currentLine++, 1 , "请选择：");
        select = wgetch(m_window);
    } while(!checkInput(select, {'1','2'}));
    if(select == '1') {

    }
    else if(select == '2') {

    }


    wgetch(m_window);
}

void Process::config()
{
    UserConfig& userConfig = UserConfig::getInstance();
    User user = userConfig.getUser();

    //已经配置过
    if (userConfig.isUserConfiged()) {
        std::string in;
        in +=  "您当前选择的词汇表为 " + user.currentVocabularyName();
        in += "，一组 " + std::to_string(user.groupSize()) + " 个。" ;
        mvwprintw(m_window,m_currentLine++,1,in.c_str());

        mvwprintw(m_window,m_currentLine++,1,"是否重新设置计划？ 1：是 2：否 ");
        mvwprintw(m_window,m_currentLine++,1,"请输入（1，2）： ");
        curs_set(2);
        char achar = wgetch(m_window);
        if(achar != '1') {
            return;
        }
    }
    //开始配置
    QList<Vocabulary> vocabularys = userConfig.getAllVocabulary();
    QHash<int, Vocabulary> vocabularyMap;
    for (int i = 0; i < vocabularys.size(); i++) {
        vocabularyMap.insert(i + 1, vocabularys[i]);
    }

    std::string inId, inSize,outPutString;
    char str[10];
    do {
        mvwprintw(m_window,m_currentLine++,1,"请选择词汇表：");
        for(int i = 0; i < vocabularys.size(); i++) {
            outPutString.append(std::to_string(i+1));
            outPutString.append("：");
            outPutString.append(vocabularys[i].vocabularyName());
            outPutString.append("，共");
            outPutString.append(std::to_string(vocabularys[i].vocabularySize()));
            outPutString.append("词  ");
            mvwprintw(m_window,m_currentLine++,1,outPutString.c_str());
            outPutString.clear();
        }
        mvwprintw(m_window,m_currentLine++,1,"请输入（1，2……)：");
        wrefresh(m_window);
        echo();
        curs_set(1);
        wgetstr(m_window, str);
        inId = str;
        mvwprintw(m_window,m_currentLine++,1,"请选择每组单词的个数 ");
        mvwprintw(m_window,m_currentLine++,1,"请输入（10， 20（推荐）， 30， 40， 50）：");
        wgetstr(m_window, str);
        inSize = str;
        noecho();
    } while (!(checkInput(inId, vocabularyMap.keys()) && checkInput(inSize, {10, 20, 30, 40, 50})));

    Vocabulary selectedVocabulary = vocabularyMap.value(std::stoi(inId));
    user.setCurrentVocabularyId(selectedVocabulary.id());
    user.setCurrentVocabularyName(selectedVocabulary.vocabularyName());
    user.setGroupSize(std::stoi(inSize));

    bool flag = userConfig.updateUserConfig(user);
    if (flag) {

        mvwprintw(m_window,m_currentLine++,1,"设置计划成功！按任意键返回…… " );
        wgetch(m_window);
    }
    else {
        mvwprintw(m_window,m_currentLine++,1,"操作失败，按任意键返回…… ");
        wgetch(m_window);
    }
}
bool Process::checkInput(std::string in, const QList<int>& list)
{
    if(in == "" ) {
        mvwprintw(m_window,m_currentLine++,1,"输入有误，请重新输入！");
        return false;
    }

    int ain = std::stoi(in);

    if (list.contains(ain)) {
        return true;
    }
    else {
        mvwprintw(m_window,m_currentLine++,1,"输入有误，请重新输入！");
        return false;
    }
}
bool Process::checkInput(char in, const QList<char>& list)
{

    if (list.contains(in)) {
        return true;
    }
    else {
        mvwprintw(m_window,m_currentLine++,1,"输入有误，请重新输入！");
        return false;
    }
}




